﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SAE_Jam
{
    class Camera
    {
        private const float ViewMargin = 0.3f;

        public float X { get; set; }
        public float Y { get; set; }

        private Player player;

        public void SetTarget(Player player)
        {
            this.player = player;
        }

        public void ScrollCamera(Viewport viewport)
        {
            float marginWidth = viewport.Width * ViewMargin;
            float marginLeft = this.X + marginWidth;
            float marginRight = this.X + viewport.Width - marginWidth;
            float marginHeight = viewport.Height * ViewMargin;
            float marginTop = this.Y + marginHeight;
            float marginBottom = this.Y + viewport.Height - marginHeight;


            float cameraTranslationX = 0f;
            float cameraTranslationY = 0f;

            if (player.animation.Position.X < marginLeft)
            {
                cameraTranslationX = player.animation.Position.X - marginLeft;
            }
            else if (player.animation.Position.X > marginRight)
            {
                cameraTranslationX = player.animation.Position.X - marginRight;
            }

            if (player.animation.Position.Y < marginTop)
            {
                cameraTranslationY = player.animation.Position.Y - marginTop;
            }
            else if (player.animation.Position.Y > marginBottom)
            {
                cameraTranslationY = player.animation.Position.Y - marginBottom;
            }

            this.X = MathHelper.Clamp(this.X + cameraTranslationX, 0f, 6400);
            this.Y = MathHelper.Clamp(this.Y + cameraTranslationY, 0f, 1120);
            Console.WriteLine(this.Y);
            Console.WriteLine("y:" + this.player.animation.Position.Y);
            Console.WriteLine("x:" + this.player.animation.Position.X);
        }
    }
}

